package net.yla.smartbus.module.ba.equipment.detachment;

import net.yla.smartbus.core.enums.DeviceTypeEnum;
import net.yla.smartbus.module.ba.equipment.dto.DeviceDto;
import net.yla.smartbus.module.ba.equipment.entity.BaDevicesDo;
import net.yla.smartbus.module.ba.vehicle.dto.VehicleDto;
import net.yla.smartbus.module.ba.vehicle.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 默苍离
 * @date 2021/6/28 10:42
 * <p>
 * ******************************************
 * @annotation: 抽离的方法
 */

@Component
public class DeviceDetachment {

    @Autowired
    private VehicleService vehicleService;



    /**
     * 设备 do转dto
     *
     * @param devicesDo
     * @return
     */
    public DeviceDto deviceDoToTto(BaDevicesDo devicesDo) {

        DeviceDto device = new DeviceDto();
        device.setNo(devicesDo.getNo());
        device.setDeviceId(devicesDo.getDeviceId());
        if (devicesDo.getPhone() != null) {
            device.setPhone(devicesDo.getPhone());
        } else {
            device.setPhone("---");
        }

        if (devicesDo.getManufacturer() != null && devicesDo.getManufacturer() != "" && devicesDo.getManufacturer().length() != 0) {
            device.setManufacturer(devicesDo.getManufacturer());
        } else {
            device.setManufacturer("---");
        }

        if (devicesDo.getBrand() != null && devicesDo.getBrand() != "" && devicesDo.getBrand().length() != 0) {
            device.setBrand(devicesDo.getBrand());
        } else {
            device.setBrand("---");
        }

        //机构名称
        device.setGroupId(devicesDo.getGroupId());

        //设备类型
        device.setDeviceType(devicesDo.getDeviceType());
        device.setDeviceTypeName(DeviceTypeEnum.getName(devicesDo.getDeviceType()));

        //绑定车辆
        if (devicesDo.getVehicleId() != 0) {
            device.setVehicleId(devicesDo.getVehicleId());
        }
        VehicleDto one = vehicleService.getOne(devicesDo.getVehicleId());
        if (one != null) {
            device.setVehicleName(one.getNo());
        } else {
            device.setVehicleName("---");
        }

        if (devicesDo.getLocatorType() != 0) {
            device.setLocatorType(devicesDo.getLocatorType());
        }

        if (devicesDo.getNetworkType() != 0) {
            device.setNetworkType(devicesDo.getNetworkType());
        }

        if (devicesDo.getCommunType() != 0) {
            device.setCommunType(devicesDo.getCommunType());
        }

        if (devicesDo.getProtocolType() != 0) {
            device.setProtocolType(devicesDo.getProtocolType());
        }

        if (devicesDo.getDifferential() != 0) {
            device.setDifferential(devicesDo.getDifferential());
        }

        device.setCameras(devicesDo.getCameras());
        device.setLimitSpeed(devicesDo.getLimitSpeed());
        device.setLimitFlow1(devicesDo.getLimitFlow1());
        device.setLimitFlow2(devicesDo.getLimitFlow2());

        return device;
    }

}
